package com.ruoyi.web.controller.teacher;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.BaseStorageDevice;
import com.ruoyi.service.IBaseStorageDeviceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 存储设备Controller
 * 
 * @author ruoyi
 * @date 2025-07-13
 */
@RestController
@RequestMapping("/system/device")
public class BaseStorageDeviceController extends BaseController
{

    @Value("${thirdPlatform.serverInfo}")
    private String serverInfo;

    @Autowired
    private IBaseStorageDeviceService baseStorageDeviceService;

    /**
     * 查询存储设备列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BaseStorageDevice baseStorageDevice)
    {
        startPage();
        List<BaseStorageDevice> list = baseStorageDeviceService.selectBaseStorageDeviceList(baseStorageDevice);
        return getDataTable(list);
    }

    /**
     * 导出存储设备列表
     */
    /*@PreAuthorize("@ss.hasPermi('system:device:export')")
    @Log(title = "存储设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BaseStorageDevice baseStorageDevice)
    {
        List<BaseStorageDevice> list = baseStorageDeviceService.selectBaseStorageDeviceList(baseStorageDevice);
        ExcelUtil<BaseStorageDevice> util = new ExcelUtil<BaseStorageDevice>(BaseStorageDevice.class);
        util.exportExcel(response, list, "存储设备数据");
    }*/

    /**
     * 获取存储设备详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(baseStorageDeviceService.selectBaseStorageDeviceById(id));
    }

    /**
     * 新增存储设备
     */
    @Log(title = "存储设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BaseStorageDevice baseStorageDevice)
    {
        return toAjax(baseStorageDeviceService.insertBaseStorageDevice(baseStorageDevice));
    }

    /**
     * 修改存储设备
     */
    @Log(title = "存储设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BaseStorageDevice baseStorageDevice)
    {
        return toAjax(baseStorageDeviceService.updateBaseStorageDevice(baseStorageDevice));
    }

    /**
     * 删除存储设备
     */
    @Log(title = "存储设备", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(baseStorageDeviceService.deleteBaseStorageDeviceByIds(ids));
    }

    @GetMapping("/getServerInfo")
    public AjaxResult getServerInfo(String ip) {
        String serverUrl = "http://"+ip+":25001/monitor/server";
        String s = HttpUtils.sendGet(serverUrl);
        JSONObject jsonObject = null;
        if (StringUtils.isNotEmpty(s)) {
            jsonObject = JSONObject.parseObject(s);
            if(jsonObject.getIntValue("code") == 200) {
                jsonObject = jsonObject.getJSONObject("data");
            }
        }
        return AjaxResult.success(jsonObject);
    }
}
